package com.boesch.electroOffice.presentation.appointment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.business.businessObject.AppointmentBusinessObject;
import com.boesch.electroOffice.business.businessObject.IdentifierBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.model.electroOffice.Identifier;
import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.wrapper.AppointmentWrapper;
import com.boesch.electroOffice.model.wrapper.SelectBooleanItem;
import com.boesch.electroOffice.utils.MessageUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

@Controller
@ManagedBean
@SessionScoped
public class AppointmentDetailController implements Serializable {

	private static final long serialVersionUID = 8195284341374063192L;
	private static final Log log = LogFactoryUtil.getLog(AppointmentDetailController.class);

	@Autowired
	private AppointmentBusinessObject appointmentBusinessObject;
	@Autowired
	private IdentifierBusinessObject identifierBusinessObject;

	private AppointmentWrapper appointmentWrapper;

	private List<SelectBooleanItem> jobIdentifiers = new ArrayList<SelectBooleanItem>();

	public void loadData(ComponentSystemEvent event) {
		String appointmentId = PortletUtils.getSessionAttribute(SessionParameter.APPOINTMENT_ID.getValue());
		try {
			if (StringUtils.isNumeric(appointmentId)) {
				appointmentWrapper = appointmentBusinessObject.readAppointmentWrapperById(Integer.parseInt(appointmentId));
			}
			if (null != appointmentWrapper) {
				retrieveSelectedJobIdentifiers();
			} else {
				MessageUtils.addErrorMessage("Auftrag konnte nicht geladen werden. ");
			}
		} catch (IllegalDataAccessException e) {
			MessageUtils.addErrorMessage("Auftrag konnte nicht geladen werden. ");
			log.error("Failed to load appointment from database: " + appointmentId, e);
		} catch (BusinessException e) {
			MessageUtils.addErrorMessage("Auftrag konnte nicht geladen werden. ");
			log.error("Failed to load appointment from database: " + appointmentId, e);
		}
	}

	private void retrieveSelectedJobIdentifiers() {
		try {
			jobIdentifiers.clear();
			List<Identifier> jobIdents = identifierBusinessObject.readIdentifiersByEntity("JOB-IDENTIFIER");
			for (Identifier identifier : jobIdents) {
				SelectBooleanItem item = new SelectBooleanItem();
				item.setItem(identifier);
				item.setDescription(identifier.getDescription());
				for (Identifier ident : appointmentWrapper.getAppointment().getJobIdentifiers()) {
					if (identifier.getId() == ident.getId()) {
						item.setSelected(true);
					}
				}
				jobIdentifiers.add(item);
			}
		} catch (IllegalDataAccessException e) {
			log.error("Failed to read jobIdentifiers from dataaccess", e);
		} catch (BusinessException e) {
			log.error("Failed to read jobIdentifiers from dataaccess", e);
		}
	}

	public AppointmentWrapper getAppointmentWrapper() {
		return appointmentWrapper;
	}

	public List<SelectBooleanItem> getJobIdentifiers() {
		return jobIdentifiers;
	}
}
